<template>
	<view class="goodList">
    <my-goods v-for="(item,index) in goodList" :key="index" :good="item"/>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: { // 请求参数
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        total: 0,
        goodList: [],
        flag: true // 节流阀
			}
		},
    onLoad (option) {
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.id || ''
      this.getGoodList()
    },
    onPullDownRefresh() { // 下拉刷新
    // 下来刷新就是将请求的参数都置为初始值，然后之前请求的也清空，然后开始重新请求一次
    this.queryObj.pagenum = 1
    this.goodList = []
    this.flag = true
    this.getGoodList()
    },
    onReachBottom() { // 到底刷新
      this.queryObj.pagenum ++
      if (this.flag) {
        if ( this.queryObj.pagenum > Math.ceil(this.total / this.queryObj.pagesize)) return uni.$showMsg('数据加载完毕')
        this.flag = false
        this.getGoodList(_ => uni.stopPullDownRefresh() )
      }
    },
    methods:{
      async getGoodList (cd) { // 获取商品列表
        const { data } = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        if (data.meta.status !== 200) return uni.$showMsg()
        cd && cd()
        this.total = data.message.total
        this.goodList = [...this.goodList,...data.message.goods]
        this.flag = true
      }
    }
    
	}
</script>

<style lang="scss">

</style>
